import { Injectable } from "@nestjs/common";
import { ValidationDto } from "./dto/validation.dto";
import { createHash } from "node:crypto";

@Injectable()
export class WechatService {
	async checkSignature({
		signature,
		echostr,
		timestamp,
		nonce,
	}: ValidationDto) {
		// 将token、timestamp、nonce三个参数进行字典序排序
		const stringArray = ["snowball", timestamp, nonce];
		const resultArray = stringArray.sort();
		// 将三个参数字符串拼接成一个字符串进行sha1加密
		const resultString = resultArray.join("");
		const hashResult = createHash("sha1").update(resultString).digest("hex");
		const isWechatHost = hashResult === signature;
		console.log("打印***hashResult", hashResult);
		console.log("打印***signature", signature);
		return isWechatHost ? echostr : "";
	}
}
